<?

$url = 'http://www.google.com/insights/search/overviewReport?' . 'geo=RU&date=' . urlencode('today 7-d') . '&cmpt=geo&content=1';
$c = curl_init($url);
curl_setopt($c, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.16) Gecko/20110323 Ubuntu/10.10 (maverick) Firefox/3.6.16');
curl_setopt($c, CURLOPT_COOKIE, '__utma=173272373.460732560.1303473367.1303473367.1303473367.1; __utmc=173272373; __utmz=173272373.1303473367.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmv=173272373.|2=soph=non_q=1,; I4SUserLocale=uk; PREF=ID=378124b2825fc93d:U=b145c6dbdcbc32a6:FF=0:TM=1298291646:LM=1298570615:GM=1:S=M0bIhhibWWQj8fQe; NID=44=d-WTssFuv_maMNNK5gsh_SnN3HupTmu-A3pgwGMdyv0vaaIokqAezEGrAu-uHMxORMkf2oI9yoVsqS_FzBMJmE05KJ698s6dfuJgYUBSW0rcdYU0TulJrYtKboQ6Obok; SID=DQAAAOoAAABeWvhwKqQ8eNZfGvtEXoh9I7LW5RHhRCFNM6Izxt_6lFnqX5ryQ4uR93ukUKqP-tfLF-qXTW3oJxaGsXn1CP1mT2EG9TcIEirhM0dFsSj1QTVfwyzc7EodHzr7uRf9oaLr0cqjBSbEEGN5sWDuRxI7E2tA7ujmfJhbojTHnNK7ov6IiNaDdhpgOE5jUoo-yqJcfYuyBbK341dTN_uFxZxkTG5909knOrBHZEwOetcjQF5ttvkPtsqmggfSDM4_JRUFOoy5uygY1lrnBzBDM_NyG2e_yDKwLQsNgo3zF7F1AfOLXI4vQnpFwp73P3tJp60; HSID=AlFgBKnB65x4eo5XD; S=izeitgeist-ad-metrics=HoHGxq5e8B0');
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$d = curl_exec($c);

preg_match_all('/trends\.PageTracker\.analyticsTrackEvent\(\'rising drilldown g link\', \'(.+?)\'\).+?<td style="width: 50%;" >(.+?)<\/td>/misu', $d, $m);

foreach ( $m[1] as $i => $title )
{
	$index = trim($m[2][$i]);

	$url = 'http://wordstat.yandex.ru/?cmd=words&page=1&text=' . urlencode($title);

	$c = curl_init($url);
	curl_setopt($c, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.16) Gecko/20110323 Ubuntu/10.10 (maverick) Firefox/3.6.16');
	curl_setopt($c, CURLOPT_COOKIE, 'yandexuid=794911061273522251; fuid01=4b8a7d6f084b049d.lTyyu1LR6fmjI8aO3-c_9x1W8zXI2ok4YtkyMRt19N85sjR2sKMg_6g5fs3ojSZKjF9pnTjvlGVPyBOZerZ-7t5Efq-ArGgdhxENUw_3t7_Gs6Z6yjwf_WB7ph5qx9PO');
	curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
	$y = curl_exec($c);
	
	preg_match('/([0-9]+)\&nbsp;показ/u', $y, $mc);
	$list[$title] = array(
		'index' => $index,
		'searches' => $mc[1]
	);

	echo "{$title}: {$index}\n";
}

file_put_contents(dirname(__FILE__) . '/data.cache', serialize($list));
echo 'ok' . "\n";